<?php
class Report extends Controller
{
	function Report()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$data['page_title']	= 'Reports';
		$data['page'] = 'report_list_v';
		
		$data['categories'] = $this->general_db_model->getAll('tbl_publication_category', 'category_name', 'parent_id = 0');
		$this->load->view('main', $data);		
	}
	
	function membersList()
	{ 
		if ($this->input->post('generate_contact_list')) 
		{ 
			// to export into csv
			//$this->exportIntoCsv(); 
			
			// to export into excel file 
			$this->load->model('park/reports_model');
			$query = $this->reports_model->get_contact_list(park_id()); 
			// loads plugin 
			$this->load->plugin('to_excel');
			// exectutes plugin function 
			to_excel($query, 'contact_list_'.date("Y-m-d")); 
		} 	 
	}
	
	function publicationList()
	{
		if($area_id = $this->input->post('category_id'))
		{

		$content = '';
		$this->load->plugin('to_excel');
		
		$content.= getHeader();
		
		$this->load->model('publication_model');
		
		if($area_id == 'all')
			$areas = $this->publication_model->get_publications_thematic_area();
		else
			$areas = $this->publication_model->get_thematic_area($area_id);
			
		//debug_array($areas);
		//$publications = $this->publication_model->get_publications();
		//debug_array($publications);die();
		if(count($areas) > 0)
		{
			if($this->input->post('uncheck_quantity'))
			{
				$content.= '<table width="100%">
							<tr>
								<td rowspan="2">Title</td>
								<td rowspan="2">Thematic Area</td>
								<td rowspan="2">Sub-Thematic Area</td>
								<td rowspan="2">Coding</td>
								<td rowspan="2">Publisher</td>
								<td colspan="2">Total Quantity of Publications for distribution</td>
								<td colspan="3">Total Quantity of Publications in box</td>
								<td colspan="2">Total Quantity of Publications in store</td>
								<td colspan="2">Total Quantity of Publications</td>
								<td rowspan="2">No of pages</td>
								<td rowspan="2">Year of Publicaiton</td>
								<td rowspan="2">ISBN Number</td>
								<td rowspan="2">Abstract</td>
								<td rowspan="2">Remarks</td>
							</tr>
							<tr>
								<td>English</td>
								<td>Nepali</td>
								<td>Location</td>
								<td>English</td>
								<td>Nepali</td>
								<td>English</td>
								<td>Nepali</td>
								<td>English</td>
								<td>Nepali</td>
							</tr>
							';
			}
			else
			{
				$content.= '<table width="100%">
							<tr>
								<td>Title</td>
								<td>Thematic Area</td>
								<td>Sub-Thematic Area</td>
								<td>Coding</td>
								<td>Publisher</td>
								<td>No of pages</td>
								<td>Year of Publicaiton</td>
								<td>ISBN Number</td>
								<td>Abstract</td>
								<td>Remarks</td>
							</tr>
							';	
			}
			foreach ($areas as $area)			
			{
				$content.= '<tr><td colspan="19" align="center" bgcolor="#ff00ff">'.$area->category_name.'</td></tr>';
				$publications = $this->publication_model->get_publications(NULL, NULL, 'a.category_id = '.$area->id);

				foreach ($publications as $publication)
				{
					$content.= '<tr>';
					$content.= '<td>'.$publication->title.'</td>';
					$content.= '<td>'.$publication->thematic_area.'</td>';
					$content.= '<td>'.$publication->sub_thematic_area.'</td>';
					$content.= '<td>'.$publication->coding.'</td>';
					$content.= '<td>'.$publication->publisher.'</td>';
					if($this->input->post('uncheck_quantity'))
					{
					$content.= '<td>'.quantity($publication->id, 'en', 'total_qty_distribution').'</td>';
					$content.= '<td>'.quantity($publication->id, 'np', 'total_qty_distribution').'</td>';
					$content.= '<td>'.$publication->location_name.'</td>';
					$content.= '<td>'.quantity($publication->id, 'en', 'total_qty_box').'</td>';
					$content.= '<td>'.quantity($publication->id, 'np', 'total_qty_box').'</td>';
					$content.= '<td>'.quantity($publication->id, 'en', 'total_qty_store').'</td>';
					$content.= '<td>'.quantity($publication->id, 'np', 'total_qty_store').'</td>';
					$content.= '<td>'.quantity($publication->id, 'en', 'total_qty').'</td>';
					$content.= '<td>'.quantity($publication->id, 'np', 'total_qty').'</td>';
					}
					$content.= '<td>'.$publication->pages.'</td>';
					$content.= '<td>'.$publication->publish_year.'</td>';
					$content.= '<td>'.$publication->isbn_number.'</td>';
					$content.= '<td>'.$publication->abstract.'</td>';
					$content.= '<td>'.$publication->remark.'</td>';
					$content.= '</tr>';
				}	
			}
			
			
			$content.= '</table>';
		} else {
			$content.= '<table><tr><td>No Publications have been added.</td></tr></table>';
		}
		
		//echo $content;
		//die();
		$content.= getFooter();
		
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=publications_list_".date('Y-m-d').".xls");
		echo $content;  
		exit();
		
		}
	
	}
	
	
	function borrowerList()
	{
		if($this->input->post('date_from') && $this->input->post('date_to'))		
		{
			$from = $this->input->post('date_from');
			$to = $this->input->post('date_to');
			
		
			$content = '';
			$this->load->plugin('to_excel');
			
			$content.= getHeader();
			$this->load->model('borrower_model');
			$borrowers = $this->borrower_model->get_borrowers_by_date($from, $to);
			
			//echo $this->db->last_query();
			//debug_array($borrowers);die();
			if(count($borrowers) > 0)
			{
				$content.= '<table width="100%">
								<tr>
									<td>First Name</td>
									<td>Last Name</td>
									<td>Email</td>
									<td>Address</td>
									<td>Phone</td>
									<td>Publication</td>
									<td>Type</td>
									<td>Refundable Quantities</td>
									<td>Non-refundable Quantities</td>
									<td>Issue Date</td>
									<td>Return Date</td>
									<td>Return Qunatities</td>
									<td>Status</td>
								</tr>
							';
				foreach($borrowers as $borrower)
				{
					$content.= '<tr>
									<td>'.$borrower->first_name.'</td>
									<td>'.$borrower->last_name.'</td>
									<td>'.$borrower->email.'</td>
									<td>'.$borrower->address1;
									
									if($borrower->address2)
										$content.= ','.$borrower->address2;
									$content.= '</td>
									<td>'.$borrower->phone1;
									
									if($borrower->phone2)
										$content.= ','.$borrower->phone2;
									$content.= '</td>
									
									<td>'.$borrower->title.'</td>
									<td>'.$borrower->type.'</td>
									<td>'.$borrower->refundable_qty.'</td>
									<td>'.$borrower->non_refundable_qty.'</td>
									
									<td>'.$borrower->issue_date.'</td>
									<td>'.$borrower->last_return.'</td>
									<td>'.$borrower->return_qty.'</td>';
									if($borrower->status == 0)
										$status = 'Not Clear';
									else
										$status = 'Clear';
									$content.= '<td>'.$status.'</td>
									
								</tr>';
				}
				
				$content.= '</table>';
							
			}
			else
			{
				$content.= '<table><tr><td>No Borrowers for the selected date.</td></tr></table>';
			}
	
			
			header("Content-type: application/x-msdownload");
			header("Content-Disposition: attachment; filename=borrowers_list_".$month.".xls");
			echo $content;  
			exit();
		}
		else
			redirect(site_url('report'));
	}
	
	function memberList()
	{
		if($this->input->post('generate_members_list'))		
		{
			$content = '';
			$this->load->plugin('to_excel');
			
			$content.= getHeader();
			$this->load->model('member_model');
			$members = $this->member_model->get_members();
			
			//echo $this->db->last_query();
			//debug_array($borrowers);die();
			if(count($members) > 0)
			{
				$content.= '<table width="100%">
								<tr>
									<td>First Name</td>
									<td>Last Name</td>
									<td>Email</td>
									<td>Address</td>
									<td>Phone</td>
									<td>Status</td>
								</tr>
							';
				foreach($members as $member)
				{
					$content.= '<tr>
									<td>'.$member->first_name.'</td>
									<td>'.$member->last_name.'</td>
									<td>'.$member->email.'</td>
									<td>'.$member->address1;
									
									if($member->address2)
										$content.= ','.$member->address2;
									$content.= '</td>
									<td>'.$member->phone1;
									
									if($member->phone2)
										$content.= ','.$member->phone2;
									$content.= '</td>';
									
									$content.= '<td>'.$member->status.'</td>
									
								</tr>';
				}
				
				$content.= '</table>';
							
			}
			else
			{
				$content.= '<table><tr><td>No Members have been added.</td></tr></table>';
			}
	
			
			header("Content-type: application/x-msdownload");
			header("Content-Disposition: attachment; filename=members_list_".date('Y-m-d').".xls");
			echo $content;  
			exit();
		}
		else
			redirect(site_url('report'));
	}
	
	function destroy_session()
	{
		$this->session->sess_destroy();	
	}
	
	function tcpdf_publication(){
            $area_id = $this->input->post('category_id');
		$this->load->model('publication_model');
                if($area_id == 'all')
			$areas = $this->publication_model->get_publications_thematic_area();
		else
			$areas = $this->publication_model->get_thematic_area($area_id);
                //debug_array($areas);
                //echo config_item('site_root').'tcpdf/examples/example_001.php';exit;
                include_once(base_url().'tcpdf/README.TXT');
		//$data['publication']= $this->publication_model->get_publication_detail($publication_id);
		//$this->load->view('tcpdf_single_publication', $data);
	   // debug($publication);exit;
		//$data['publicationBorrowerList']=$this->borrower_model->get_active_borrower_list($id);
		//$data['page_title']	= 'Publication Details';
	   // $data['page'] = 'publication_detail_v';
	  
		
	}
}
?>